package com.itheima.mapper;

import com.itheima.pojo.Order;
import com.itheima.pojo.User;
import com.itheima.pojo.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

// 这个接口中的方法都是用来操作user表
public interface UserMapper {
    // 通过用户ID查询用户的基本信息
    User findUser(int uid);

//    // 通过用户ID查询用户扩展信息
    UserInfo findUserInfo(int uid);
//    User findUserAll(int uid);
    Order findUserOrders(int uid);
    //注解来进行一对一的查询
    @Select("SELECT *FROM USER WHERE id=#{uid};")
    @Results({
            @Result(property = "id" ,column = "id"),
            @Result(property = "username" ,column = "username"),
            @Result(property = "birthday" ,column = "birthday"),
            @Result(property = "sex" ,column = "sex"),
            @Result(property = "address" ,column = "address"),
            @Result(property = "userInfo",column = "id",one = @One(select = "findUserInfoByld")),
            @Result(property = "orders",column = "id",many = @Many(select = "findOrders"))
    })
    User findUserByld(int uid);
    @Select("SELECT *FROM user_info WHERE id=#{uid};")
    UserInfo findUserInfoByld(int uid);
    @Select("SELECT *FROM `order` WHERE user_id=#{uid};")
    List<Order> findOrders(int uid);


    //使用注解进行增删改
    @Delete("DELETE FROM USER WHERE id=#{uid};")
    void deleteByIdUser(int uid);

    @Insert("INSERT INTO USER VALUES(NULL,#{username},#{birthday},#{sex},#{address});")
    void insertUser(User user);
    @Update("UPDATE USER SET username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} WHERE id=#{id};")
    void updateIdUser(User user);






}
